home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex9-3.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  864b  |  40 lines

  1. // ex9-3.c -- Correct handling of member pointers
  2. //            to class instances
  3.  
  4. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex9-3.c,v 3.0 90/05/15 22:46:36 kgorlen Rel $
  5.  
  6. #include "String.h"
  7.  
  8. class X {
  9.     String* s;
  10. public:
  11.     X(const char* t="")     { s = new String(t); }
  12.     X(const X& x)           { s = new String(*x.s); }
  13.     void operator=(const X&);
  14.     ~X()                    { delete s; }
  15.     void set(const char* t) { *s = t; }
  16.     friend ostream& operator<<(ostream& strm, X& x) {
  17.         strm << *x.s;
  18.         return strm;
  19.     }
  20. };
  21.  
  22. void X::operator=(const X& x)
  23. {
  24.     if (this == &x) return;
  25.     delete s;
  26.     s = new String(*x.s);
  27. }
  28.  
  29. main()
  30. {
  31.     X a = "abc";
  32.     X b = a;
  33.     X c;
  34.     c = a;
  35.     a.set("xyz");
  36.     cout << "a=" << a << endl;
  37.     cout << "b=" << b << endl;
  38.     cout << "c=" << c << endl;
  39. }
  40.